package com.ruoyi.project.module.project.service;

import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 接口项目 服务层实现
 * 
 * @author ruoyi
 * @date 2018-09-28
 */
@Service
public class ProjectServiceImpl implements IProjectService 
{
	@Autowired
	private ProjectMapper projectMapper;

	/**
     * 查询接口项目信息
     * 
     * @param apiProjectnameId 接口项目ID
     * @return 接口项目信息
     */
    @Override
	public Project selectProjectById(Integer apiProjectnameId)
	{
	    return projectMapper.selectProjectById(apiProjectnameId);
	}
	
	/**
     * 查询接口项目列表
     * 
     * @param project 接口项目信息
     * @return 接口项目集合
     */
	@Override
	public List<Project> selectProjectList(Project project)
	{
	    return projectMapper.selectProjectList(project);
	}
	
    /**
     * 新增接口项目
     * 
     * @param project 接口项目信息
     * @return 结果
     */
	@Override
	public int insertProject(Project project)
	{
		String userName = ShiroUtils.getUser().getLoginName();
		Project parentProject = projectMapper.selectProjectById(project.getParentId());
		project.setCreateBy(userName);
		project.setCreateTime(new Date());
		project.setUpdateBy(userName);
		project.setUpdateTime(new Date());
		if(parentProject!=null){
			project.setAncestors(parentProject.getAncestors() + "," + project.getParentId());
		}else {
			project.setAncestors("0");
		}
		return projectMapper.insertProject(project);
	}
	
	/**
     * 修改接口项目
     * 
     * @param project 接口项目信息
     * @return 结果
     */
	@Override
	public int updateProject(Project project)
	{
		String userName = ShiroUtils.getUser().getLoginName();
		project.setCreateBy(null);
		project.setCreateTime(null);
		project.setUpdateBy(userName);
		project.setUpdateTime(new Date());
		Project info = projectMapper.selectProjectById(project.getParentId());
		if(info!=null){
			project.setAncestors(info.getAncestors() + "," + project.getParentId());
		}
	    return projectMapper.updateProject(project);
	}

	/**
     * 删除接口项目对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteProjectByIds(String ids)
	{
		return projectMapper.deleteProjectByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除接口项目
	 *
	 * @param apiProjectnameId 接口项目ID
	 * @return 结果
	 */
	@Override
	public int deleteProjectById(Integer apiProjectnameId){
		return projectMapper.deleteProjectById(apiProjectnameId);
	}

	/**
	 * 项目列表树,用于接口模块中显示
	 * @return
	 */
	@Override
	public List<Map<String,Object>> selectProjectTree(){
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		List<Project> projectList = selectProjectList(new Project());
		trees = getTrees(projectList, false, null);
		return trees;
	}

	@Override
	public boolean hasChilds(Integer parentId) {
	    int childSize=projectMapper.childSize(parentId);
		return childSize==0?false:true;
	}

	/**
	 * 对象转部门树
	 *
	 * @param projectList 部门列表
	 * @param isCheck 是否需要选中
	 * @param roleDeptList 角色已存在菜单列表
	 * @return
	 */
	public List<Map<String, Object>> getTrees(List<Project> projectList,boolean isCheck, List<String> roleDeptList)
	{

		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		for (Project project : projectList)
		{
			Map<String, Object> projectMap = new HashMap<String, Object>();
			projectMap.put("id", project.getApiProjectnameId());
			projectMap.put("pId", project.getParentId());
			projectMap.put("name", project.getDescription());
			projectMap.put("title", project.getDescription());
			if (isCheck)
			{
				projectMap.put("checked", roleDeptList.contains(project.getApiProjectnameId() + project.getDescription()));
			}
			else
			{
				projectMap.put("checked", false);
			}
			trees.add(projectMap);
		}
		return trees;
	}


	@Override
	public int maxOrderNum( Long parentId) {
		return projectMapper.maxOrderNum(parentId);
	}
}
